<?php
/**
 * 用户管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/26
 * Time: 11:34
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\BoolStatus;
use app\common\model\Member as MemberModel;
use app\admin\validate\Member as MemberValidate;

class Member extends AdminBase
{
    /**
     * 用户列表
     * @return mixed
     */
    public function index()
    {
        $memberModel = new MemberModel();
        $list        = $memberModel->paginate($this->pageSize);

        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign('page', $page);

        $status = BoolStatus::getLables();
        $this->assign('status', $status);
        return $this->fetch('member/index');
    }

    /**
     * 设置用户信息
     * @return mixed
     */
    public function set()
    {
        $id          = $this->request->param('id', 0, 'int');
        $memberModel = new MemberModel();
        if ($this->request->isPost()) {

            $name   = $this->request->param('name');
            $email  = $this->request->param('email');
            $status = $this->request->param('status');

            //验证数据
            $validate = new MemberValidate();
            if (!$validate->check(['name' => $name, 'email' => $email, 'status' => $status])) {
                $this->error($validate->getError());
            }
            $currentTime = time();
            if (!$id) {
                //新增
                $count = $memberModel->where('name', '=', $name)->count();
                if ($count) {
                    $this->error('该用户名已存在，请输入其他的用户名~~');
                }
                $memberModel->created_time = $currentTime;
                $memberModel->is_admin     = 0;
            } else {
                //编辑
                $count = $memberModel->where('name', '=', $name)->whereNotIn('id', $id)->count();
                if ($count) {
                    $this->error('该用户名已存在，请输入其他的用户名~~');
                }
                $memberModel = $memberModel->find($id);

                if ($this->currentUser && !$this->currentUser['is_admin'] && $memberModel->is_admin) {
                    $this->error('您无权编辑超级管理员的信息~~');
                }
            }
            $memberModel->updated_time = $currentTime;
            $memberModel->name         = $name;
            $memberModel->email        = $email;
            $memberModel->status       = $status;

            $result = $memberModel->save();
            if ($result) {
                $this->success('操作成功~~', 'member/index');
            } else {
                $this->error('操作失败~~');
            }
        }
        $member = null;
        if ($id) {
            $member = $memberModel->find($id);
        }
        $this->assign('member', $member);

        $status = BoolStatus::getLables();
        $this->assign('status', $status);
        return $this->fetch('member/set');
    }
}